import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/contract_controller.dart';
import '../l10n/app_localizations.dart';
import '../model/check_in.dart';

class SignInRewards extends StatelessWidget {
  const SignInRewards({super.key});

  @override
  Widget build(BuildContext context) {
    final ContractController controller = Get.find();
    final l10n = AppLocalizations.of(context);

    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 标题
          Text(
            l10n.signInToReceiveRewards,
            style: TextStyle(
              color: const Color(0xFF05FFFD),
              fontSize: 20.sp,
              fontWeight: FontWeight.w600,
            ),
          ),
          SizedBox(height: 4.h),
          Text(
            l10n.increaseRewardsAndGiftsEveryDay,
            style: TextStyle(color: const Color(0xFF05FFFD), fontSize: 16.sp),
          ),
          SizedBox(height: 16.h),

          // 签到奖励网格（使用真实数据）
          Obx(() {
            final CheckIn? data = controller.checkIn.value;
            if (data == null) {
              return Container(
                height: 100.h,
                alignment: Alignment.center,
                child: const CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF00FFFF)),
                ),
              );
            }

            return Container(
              padding: EdgeInsets.all(16.w),
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage("assets/images/bg_check_in.png"),
                  fit: BoxFit.fill,
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children:
                    data.details.asMap().entries.map((entry) {
                      final int day = entry.key + 1;
                      final CheckInDetail detail = entry.value;
                      final bool isCompleted = detail.is_checkin == 1;
                      final bool isCurrent =
                          detail.checkin_date == data.current_date;

                      final bool isDim = !isCompleted && !isCurrent;
                      final String iconPath =
                          isCurrent
                              ? (isCompleted
                                  ? "assets/images/icon_check_in_disable.png"
                                  : "assets/images/icon_check_in_enable.png")
                              : "assets/images/icon_bit_coin.png";
                      // print(
                      //   "key: ${entry.key},isCompleted: $isCompleted,isCurrent: $isCurrent, iconPath: $iconPath",
                      // );
                      return Expanded(
                        child: GestureDetector(
                          onTap: isCurrent ? controller.performCheckIn : null,
                          child: Opacity(
                            opacity: isDim ? 0.45 : 1.0,
                            child: Container(
                              margin: EdgeInsets.symmetric(horizontal: 2.w),
                              padding: EdgeInsets.symmetric(vertical: 8.h),
                              child: Column(
                                children: [
                                  Image(
                                    image: AssetImage(iconPath),
                                    width: 32.r,
                                    height: 32.r,
                                  ),
                                  SizedBox(height: 4.h),
                                  Text(
                                    '${l10n.day} $day',
                                    style: TextStyle(
                                      color: const Color(0xFFFFFFFF),
                                      fontSize: 12.sp,
                                    ),
                                  ),
                                  SizedBox(height: 4.h),
                                  Text(
                                    '${detail.hashrate} Gh/s',
                                    style: TextStyle(
                                      color:
                                          isCompleted
                                              ? Colors.white
                                              : isCurrent
                                              ? const Color(0xFF00FFFF)
                                              : Colors.white70,
                                      fontSize: 9.sp,
                                      fontWeight: FontWeight.w600,
                                    ),
                                  ),
                                  SizedBox(height: 2.h),
                                  Text(
                                    '24 ${l10n.hours}',
                                    style: TextStyle(
                                      color:
                                          isCompleted
                                              ? Colors.white70
                                              : isCurrent
                                              ? const Color(
                                                0xFF00FFFF,
                                              ).withOpacity(0.7)
                                              : Colors.white54,
                                      fontSize: 8.sp,
                                      fontWeight: FontWeight.w400,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      );
                    }).toList(),
              ),
            );
          }),
        ],
      ),
    );
  }
}
